package com.baizhi.controller;

import com.baizhi.entity.Backlog;
import com.baizhi.service.BacklogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@Tag(name="待办事项管理",description = "待办事项管理的控制器")
public class BacklogController {

    @Autowired
    private BacklogService backlogService;

    @GetMapping("/backlog/del")
    public ResponseEntity<String> deleteBacklogById(@RequestParam(value = "id",required = true) Integer id){
        System.out.println("id=========="+id);
        boolean flag = backlogService.removeById(id);
        if(flag){
            return new ResponseEntity<>("成功",HttpStatusCode.valueOf(200));
        }else{
            return new ResponseEntity<>("失败",HttpStatusCode.valueOf(500));
        }
    }

    /**
     * 添加待办事项的方法
     * @param backlog
     * @return
     */
    @PostMapping("/backlog/add")
    @Operation(description = "添加待办事项的方法")
    public ResponseEntity<String> addBacklog(@RequestBody Backlog backlog){
        backlog.setGmtCreate(new Date());
        backlog.setGmtModified(new Date());
        boolean flag = backlogService.save(backlog);
        if(flag){
            return new ResponseEntity("成功",HttpStatusCode.valueOf(200));
        }else{
            return new ResponseEntity("失败",HttpStatusCode.valueOf(500));
        }
    }

    /**
     * 获取所有的待办事项
     * @return
     */
    @GetMapping("/backlog/all")
    @Operation(description = "根据用户Id查询待办事项")
    public List<Backlog> getBacklogsByUserId(@RequestParam(value = "userId",required = true) Integer userId){
        System.out.println(userId);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);

        List<Backlog> list = backlogService.list(wrapper);
        return list;
    }
}
